package com.cc.gulimall.order.listener;


import com.cc.gulimall.order.entity.OrderEntity;
import com.cc.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * <p>Title: OrderCloseListener</p>
 * Description：
 * date：2020/7/4 1:36
 */
@Service
@RabbitListener(queues = "${myRabbitmq.MQConfig.queues}")
public class OrderCloseListener {

    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void listener(OrderEntity entity, Channel channel, Message message) throws IOException {
        try {
            //项目中都设置的是2min到期
            // 可细化：如果我们的订单关闭时间设置的小于支付宝自动收单时间，我们可以手动调用支付宝收单在这里。
            // 不要让付款成功。这里我们只是假设设置两者时间一致
            orderService.closeOrder(entity);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }
}
